package com.netdisk.controller;
/**
 * @author chenlong
 * 2013-6-2
 * 文件系统类,对这个页面的显示初始化操作.
 */
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.netdisk.entity.FileSystem;
import com.netdisk.entity.User;
import com.netdisk.service.impl.FileSystemService;
import com.opensymphony.xwork2.ActionSupport;

@Component("fileSystemDisplayAction")
@Scope("prototype")
public class FileSystemDisplayAction extends ActionSupport{
	
	private FileSystemService fileSystemService;
	
	@Resource(name="fileSystemService")
	public void setFileSystemService(FileSystemService fileSystemService) {
		this.fileSystemService = fileSystemService;
	}


	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		
		if(null == session.getAttribute("user")){
			return INPUT;
		}
		User user = (User) session.getAttribute("user");
		String logined = (String) session.getAttribute("logined");
		// 找到根目录
		FileSystem  fs = null;
		List<FileSystem> lf = null; 
		if("enterprise".equals(logined)){// 企业用户
			fs = fileSystemService.findWithCompanyId(user.getCompanyId(),  "/").get(0);
			lf = fileSystemService.findCompanyParentDir(user.getCompanyId(), fs.getStoreName());
		}else{// 普通客户
			fs = fileSystemService.findWithOwnerId(user.getId(), "/").get(0);
			lf = fileSystemService
					. findOwnerParentDir(user.getId(), fs.getStoreName());
		}
		request.setAttribute("parentDir", "..");// 根目录没有上级目录
		request.setAttribute("currentDir", fs.getStoreName());// 当前路径
		request.setAttribute("storeDir", fs.getStoreName());// 方便下载，当前用户的文件根路径在WEB-INF下
		request.setAttribute("lf", lf);// 根目录没有上级目录
		return super.execute();
	}

}
